package teamone;

import robocode.TeamRobot;
import java.lang.Math;

public class Scanning{
	
	//a function for evaluating the distance between two points
	public static double getDistance(double myXCoordinate, double myYCoordinate, double xCoordinate, double yCoordinate){
		double xDistance = xCoordinate - myXCoordinate;
		double yDistance = yCoordinate - myYCoordinate;
		double distance = Math.sqrt(yDistance*yDistance + xDistance*xDistance);
		return distance;
	}
	
	//a function for moving the robot to specific coordinates 
	public static void moveTo(SimpleDrone MyRobot, double xCoordinate, double yCoordinate){
		double myXCoordinate = MyRobot.getX();
		double myYCoordinate = MyRobot.getY();
		if((myXCoordinate == xCoordinate) && (myYCoordinate == yCoordinate)){
			
		}
		else{
			double yDistance = Math.abs(yCoordinate - myYCoordinate);
			double distance = getDistance(myXCoordinate, myYCoordinate, xCoordinate, yCoordinate);
			double arc = Math.abs(Math.toDegrees(Math.asin(yDistance/distance)));
			if(myXCoordinate >= xCoordinate ){
				if(myYCoordinate >= yCoordinate){
					arc = 270 - arc;
				}
				else{
					arc = 270 + arc;
				}
			}
			else{
				if(myYCoordinate >= yCoordinate){
					arc = 90 + arc;
				}
				else{
					arc = 90 - arc;
				}
			}
			MyRobot.turnLeft(MyRobot.getHeading() - arc);
			MyRobot.ahead(distance);
			/*if((xCoordinate < (MyRobot.wallMargin))||(xCoordinate > (MyRobot.getBattleFieldWidth() - MyRobot.wallMargin))
					||(yCoordinate < MyRobot.wallMargin)||(yCoordinate > (MyRobot.getBattleFieldHeight() - MyRobot.wallMargin))){
				MyRobot.ahead(distance - MyRobot.wallMargin);
			}
			else{
				MyRobot.ahead(distance);
			}*/
		}
	}
	
	//a function for choosing the next enemy to attack
	public static void decideOnNextEnemy(SimpleDrone MyRobot){
		double healthPoints = 1000.0;
		int enemyNumber = -1;
		for (int i = 0; i < MyRobot.enemyHealthArray.length; i++){
			if ((MyRobot.enemyHealthArray[i] < healthPoints)&&(MyRobot.enemyHealthArray[i] != 0.0)){
				healthPoints = MyRobot.enemyHealthArray[i];
				enemyNumber = i;
			}
		}
		if(enemyNumber == -1){
			System.out.println(enemyNumber + "shit");
		}
		else{
			MyRobot.enemyName = MyRobot.enemyNamesArray[enemyNumber];
			System.out.println("decidedOnEnemy: " + MyRobot.enemyName);
			MyRobot.scanningMode = false;
			MyRobot.battleMode = true;
			//Battle.battleEnemy(MyRobot);
		}
	}
	
	//a function for scanning 360 degrees around a certain spot 
	public static void scanArea(SimpleDrone MyRobot, double scanXCoordinate, double scanYCoordinate){
		//MyRobot.scanningMode = true;
		Movement.move(MyRobot, scanXCoordinate, scanYCoordinate);
		//MyRobot.scanningMode = true;
		MyRobot.turnRadarLeft(90);
		MyRobot.turnRadarLeft(90);
		MyRobot.turnRadarLeft(90);
		MyRobot.turnRadarLeft(90);
		//MyRobot.scanningMode = false;
		//decideOnNextEnemy(MyRobot);
	}
	
}

